package com.mj.mafeed.adapter;

import java.util.List;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mj.mafeed.R;
import com.mj.mafeed.bean.ProductJSON;
import com.mj.mafeed.config.Constants;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

/**
 * 产品列表
 * @author zhaominglei
 * @date 2014-12-16
 * 
 */
public class ProductAdapter extends BaseAdapter {
	private List<ProductJSON> products;
	private Context context;
	@SuppressWarnings("unused")
	private ProgressBar progressBar;
	private ImageLoader imageLoader = null;
	private DisplayImageOptions options = null;

	@SuppressWarnings("deprecation")
	public ProductAdapter(Context context, List<ProductJSON> products) {
		super();
		this.context = context;
		this.products = products;
		imageLoader = ImageLoader.getInstance();
		imageLoader.init(ImageLoaderConfiguration.createDefault(context));

		options = new DisplayImageOptions.Builder()
				.displayer(new RoundedBitmapDisplayer(0xff000000, 10))
				.cacheInMemory().cacheOnDisc().build();
	}

	public List<ProductJSON> getProducts() {
		return products;
	}

	public void setProducts(List<ProductJSON> products) {
		this.products.addAll(products);
	}
	@Override
	public int getCount() {
		return products.size();
	}

	@Override
	public Object getItem(int position) {
		return products.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		RelativeLayout relativeLayout;
		if (convertView != null) {
			relativeLayout = (RelativeLayout) convertView;
		} else {
			relativeLayout = (RelativeLayout) View.inflate(context,
					R.layout.product_item, null);
		}
		progressBar = (ProgressBar) relativeLayout.findViewById(R.id.product_item_progress);
		ImageView imageView = (ImageView) relativeLayout
				.findViewById(R.id.product_item_image);

		imageLoader.displayImage(Constants.SERVER_DOWNLOAD_URL+products.get(position).getImageUrl(),
				imageView, options);
		((TextView) relativeLayout
				.findViewById(R.id.product_item_template_name))
				.setText(products.get(position).getName());
//		((TextView) relativeLayout
//				.findViewById(R.id.product_list_template_memo))
//				.setText(products.get(position).getMemo());
		((TextView) relativeLayout
				.findViewById(R.id.product_item_template_price))
				.setText(String.valueOf(products.get(position).getPrice()));
		((TextView) relativeLayout
				.findViewById(R.id.product_item_template_id))
				.setText(String.valueOf(products.get(position).getProductId()));
		return relativeLayout;
	}

}
